package 门面模式.特产的选购界面;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

/**
 * 本实例的外观角色 WySpecialty 是 JPanel 的子类，
 * 它拥有 8 个子系统角色 Specialty1~Specialty8，它们是图标类（ImageIcon）的子类对象，用来保存该婺源特产的图标
 * 外观类（WySpecialty）用 JTree 组件来管理婺源特产的名称，
 * 并定义一个事件处理方法 valueClianged(TreeSelectionEvent e)，当用户从树中选择特产时，
 * 该特产的图标对象保存在标签（JLabd）对象中。
 * @author: TangLiang
 * @date: 2021/2/3 20:15
 * @since: 1.0
 */
public class WySpecialtyFacade {
    public static void main(String[] args) {
        JFrame f = new JFrame("外观模式: 婺源特产选择测试");
        Container cp = f.getContentPane();
        WySpecialty wys = new WySpecialty();
        JScrollPane treeView = new JScrollPane(wys.tree);
        JScrollPane scrollpane = new JScrollPane(wys.label);
        JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, treeView, scrollpane); //分割面版
        splitpane.setDividerLocation(230);     //设置splitpane的分隔线位置
        splitpane.setOneTouchExpandable(true); //设置splitpane可以展开或收起
        cp.add(splitpane);
        f.setSize(650, 350);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class WySpecialty extends JPanel implements TreeSelectionListener {
    private static final long serialVersionUID = 1L;
    final JTree tree;
    JLabel label;
    private Specialty1 s1 = new Specialty1();
    private Specialty2 s2 = new Specialty2();
    private Specialty3 s3 = new Specialty3();
    private Specialty4 s4 = new Specialty4();
    private Specialty5 s5 = new Specialty5();
    private Specialty6 s6 = new Specialty6();
    private Specialty7 s7 = new Specialty7();
    private Specialty8 s8 = new Specialty8();
    WySpecialty() {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("婺源特产");
        DefaultMutableTreeNode node1 = null, node2 = null, tempNode = null;
        node1 = new DefaultMutableTreeNode("婺源四大特产（红、绿、黑、白）");
        tempNode = new DefaultMutableTreeNode("婺源荷包红鲤鱼");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源绿茶");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源龙尾砚");
        node1.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源江湾雪梨");
        node1.add(tempNode);
        top.add(node1);
        node2 = new DefaultMutableTreeNode("婺源其它土特产");
        tempNode = new DefaultMutableTreeNode("婺源酒糟鱼");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源糟米子糕");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源清明果");
        node2.add(tempNode);
        tempNode = new DefaultMutableTreeNode("婺源油煎灯");
        node2.add(tempNode);
        top.add(node2);
        tree = new JTree(top);
        tree.addTreeSelectionListener(this);
        label = new JLabel();
    }
    public void valueChanged(TreeSelectionEvent e) {
        if (e.getSource() == tree) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) return;
            if (node.isLeaf()) {
                Object object = node.getUserObject();
                String sele = object.toString();
                label.setText(sele);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setVerticalTextPosition(JLabel.BOTTOM);
                sele = sele.substring(2, 4);
                if (sele.equalsIgnoreCase("荷包")) label.setIcon(s1);
                else if (sele.equalsIgnoreCase("绿茶")) label.setIcon(s2);
                else if (sele.equalsIgnoreCase("龙尾")) label.setIcon(s3);
                else if (sele.equalsIgnoreCase("江湾")) label.setIcon(s4);
                else if (sele.equalsIgnoreCase("酒糟")) label.setIcon(s5);
                else if (sele.equalsIgnoreCase("糟米")) label.setIcon(s6);
                else if (sele.equalsIgnoreCase("清明")) label.setIcon(s7);
                else if (sele.equalsIgnoreCase("油煎")) label.setIcon(s8);
                label.setHorizontalAlignment(JLabel.CENTER);
            }
        }
    }
}
class Specialty1 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty1() {
        super("src/images/门面模式/Specialty11.jpg");
    }
}
class Specialty2 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty2() {
        super("src/images/门面模式/Specialty12.jpg");
    }
}
class Specialty3 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty3() {
        super("src/images/门面模式/Specialty13.jpg");
    }
}
class Specialty4 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty4() {
        super("src/images/门面模式/Specialty14.jpg");
    }
}
class Specialty5 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty5() {
        super("src/images/门面模式/Specialty21.jpg");
    }
}
class Specialty6 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty6() {
        super("src/images/门面模式/Specialty22.jpg");
    }
}
class Specialty7 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty7() {
        super("src/images/门面模式/Specialty23.jpg");
    }
}
class Specialty8 extends ImageIcon {
    private static final long serialVersionUID = 1L;
    Specialty8() {
        super("src/images/门面模式/Specialty24.jpg");
    }
}